// JavaScript Document
$(document).ready(function(){
	//我的订单，我的窝窝券
	$(".userr_dd .userr_tdbox1").each(function(index) {
		var $this = $(this),
			datc = $this.find("a[date = 'user_kq']"),
			daxs = $this.find("div.userr_wwqbox"),
			dagb = $this.find("em.userr_seeclose");
			$this.css("z-index",100-index);
			datc.click(function(){
				daxs.show();
				});
			dagb.click(function(){
				daxs.hide();
				});
	});
	$(".userr_table tr").hover(function(){
		var $this = $(this),
			qimgbg = $this.find("em.userr_huiq");
			$this.css("background","#f2f2f2");
			qimgbg.addClass("userr_huiq1");
		},function(){
			var $this = $(this),
			qimgbg = $this.find("em.userr_huiq");
			$this.css("background","#ffffff");
			qimgbg.removeClass("userr_huiq1");
			});
	//我的留言
	$(".userr_lybox:even").css("background","#f2f2f2");
	$(".userr_lybox:odd").css("background","#ffffff");
	$(".userr_ej:odd").css("background-position","-734px -90px");
	//账户充值
	(function(){
		var oLi = $("#czlist .userr_ccul li.userr_ccli"),
			aImg = oLi.find("img"),
			aRadio = oLi.find("input");
		oLi.click(function(){
			var $_this = $(this),
				img = $_this.find("img"),
				radio = $_this.find("input");
			aRadio.attr("checked",false);
			aImg.css("border-color","#dddddd");
			radio.attr("checked",true);
			img.css("border-color","#e54007");
		});
		
	})();
	//修改密码
	var myPassword = function(){
		var passForm = $("#mimaForm"),
			Ypassword = passForm.find("input[name='yPassword']"),
			Fpassword = passForm.find("input[name='fPassword']"),
			Spassword = passForm.find("input[name='sPassword']"),
			Yerr = passForm.find("dd[date='yerr']"),
			Ferr = passForm.find("dd[date='ferr']"),
			Serr = passForm.find("dd[date='serr']"),
			Allerr = passForm.find("dd.userr_zhdd2"),
			Allinput = passForm.find("input.userr_ahddin"),
			Alldl = passForm.find("dl.userr_zhdl")
			Subpass = $("#sbmima");
			//focus取消提示
			Alldl.each(function(index) {
				Allinput.eq(index).focus(function(){
					Allerr.eq(index).hide();
					});
			});
			//密码判断
			Ypassword.blur(function(){
				None($(this).val(),Yerr,"原密码不能为空！");
				});
			Fpassword.blur(function(){
				var $this = $(this);
				None($this.val(),Ferr,"新密码不能为空！");
				Lenght($this.val(),Ferr,"密码需要6-16个字符！");
				});
			Spassword.blur(function(){
				var $this = $(this);
				None($this.val(),Serr,"确认密码不能为空！");
				Same(Fpassword.val(),$this.val(),Serr,"新密码和确认密码不一致！");
				});
			//判断空
			function None(v,err,str){
				if(v == ""){
					err.show();
					err.html(str);
					return false;
					};
					return true;
				};
			//判断密码是否相同
			function Same(fv,sv,err,str){
				if(fv != sv){
					err.show();
					err.html(str);
					return false;
					};
					return true;
				};
			//判断长度
			function Lenght(v,err,str){
				if(v.length>0&&v.length<6){
					err.show();
					err.html(str);
					return false;
					};
					return true;
				};
			//提交
			Subpass.click(function(){
				var subypass = None(Ypassword.val(),Yerr,"原密码不能为空！"),
			 		subFpassn = None(Fpassword.val(),Yerr,"新密码不能为空！"),
			 		subFpassl = None(Fpassword.val(),Yerr,"密码需要6-16个字符！"),
		 			subSpassn = None(Spassword.val(),Yerr,"确认密码不能为空！"),
			 		subSpasss = None(Spassword.val(),Yerr,"新密码和确认密码不一致！");
					if(subypass&&subFpassn&&subFpassl&&subSpassn&&subSpasss){
						return true;
						passForm.submit();
						}else{
							return false;
							};
				});
		};
		myPassword();
	//修改收货地址
	var $addressBtn = $(".userr_adddza");
	var $addressForm = $(".userr_zhboxdz");
	var $boxMarginBottom = $(".userr_adddz");
	var $submitBtn = $("#usersub");
	//显示form表单
	$addressBtn.click(function(){
		$boxMarginBottom.css("margin-bottom","0")
		$addressForm.show();
	});
	var $form = $("#addressform");
	var $username = $form.find("input[name='ucname']"),//收件人姓名
		$usernameMSG = $username.parent().nextAll("dd");
	var $address = $form.find("input[name='usjdaddress']"),//街道地址
		$addressMSG = $address.parent().nextAll("dd");
	var $youbian = $form.find("input[name='ucyoubian']"), //邮编
		$youbianMSG = $youbian.parent().nextAll("dd");
	var $phonetel = $form.find("input[name='ucphone']"),//手机
		$phonetelMSG = $phonetel.parent().nextAll("dd");
	var oCheck = {
		// v 值 , oMsg 提示对象 , text 提示语言
		checkNull : function(v,oMsg,text){
			if (v.length==0){
				oMsg.html(text).show();
				return false
			}else{
				return true;
			}
		},
		// v 值 , oMsg 提示对象 , text 提示语,oTest正则表达式
		checkTest : function(v,oMsg,text,oTest){
			if (!oTest.test(v)){
				oMsg.html(text).show();
				return false
			}else{
				return true;
			};
		}
		
	};
	//收件人判断
	$username.blur(function(){
		var v = $.trim(this.value);
		oCheck.checkNull(v,$usernameMSG,"收件人不能为空");
	});
		$username.focus(function(){
			$usernameMSG.html("").hide();
		});
	//地址判断
	$address.blur(function(){
		var v = $.trim(this.value);
		oCheck.checkNull(v,$addressMSG,"地址不能为空");
	});
		$address.focus(function(){
			$addressMSG.html("").hide();
		});
	//邮编判断
	$youbian.blur(function(){
		
		var v = $.trim(this.value);
		if (v.length==0){
			return;
		}
		oCheck.checkTest(v,$youbianMSG,"邮编格式不对",/^[1-9][0-9]{5}$/);
	});
		$youbian.focus(function(){
			$youbianMSG.html("").hide();
		});
	//电话判断
	$phonetel.blur(function(){
		var v = $.trim(this.value);
		oCheck.checkTest(v,$phonetelMSG,"电话格式不对",/^0?1(3|5|8)\d{9}$/);
	});
	$phonetel.blur(function(){
		var v = $.trim(this.value);
		oCheck.checkNull(v,$phonetelMSG,"电话不能为空");
	});
		$phonetel.focus(function(){
			$phonetelMSG.html("").hide();
		});
	//提交表单
	$submitBtn.click(function(){
		var $usernameVal = $.trim($username.val()),
			$addressVal = $.trim($address.val()),
			$youbianVal = $.trim($youbian.val());
			$phonetelVal = $.trim($phonetel.val());
		
		
		var bUsername = oCheck.checkNull($usernameVal,$usernameMSG,"收件人不能为空"),
			bAddress = oCheck.checkNull($addressVal,$addressMSG,"地址不能为空");
			if ($youbianVal.length>0){
				var bYoubian = oCheck.checkTest($youbianVal,$youbianMSG,"邮编格式不对",/^[1-9][0-9]{5}$/);
			}else{
				bYoubian = true;
			}
			
		var bPhonetel = oCheck.checkNull($phonetelVal,$phonetelMSG,"电话不能为空"),
			bPhonetel = oCheck.checkTest($phonetelVal,$phonetelMSG,"电话格式不对",/^0?1(3|5|8)\d{9}$/);
			
			
		if (bUsername && bAddress && bYoubian && bPhonetel && bPhonetel){
			/*$.post("test.php", {},
			function(data) {
				alert(date);
			});*/
			$youbianVal = $youbianVal || "&nbsp;";
			var html = 
			'<tr>'+
			'	<td align="center" valign="middle" class="userr_td">'+$usernameVal+'</td>'+
			'	<td align="center" valign="middle" class="userr_td">'+
			'		北京&nbsp;&nbsp;海淀区&nbsp;&nbsp;农大南路 1号 硅谷亮城 9号楼'+
			'	</td>'+
			'	<td align="center" valign="middle" class="userr_td">'+$youbianVal+'</td>'+
			'	<td align="center" valign="middle" class="userr_td">'+$phonetelVal+'</td>'+
			'	<td class="userr_td userr_td1">'+
			'		<div class="userr_tdbox2">'+
			'			<a class="userr_tdbeblu" title="默认地址" href="#">默认地址</a><br>'+
			'			<em class="userr_tdbeblu">'+
			'				<a title="修改" class="userr_tdbeblu" href="#">修改</a>/'+
			'				<a title="删除" class="userr_tdbeblu" href="#">删除</a>'+
			'			</em>'+
			'		</div>'+
			'	</td>'+
			'</tr>';
			$(".userr_table1").append(html);
		}
		
	});
	
	
	
	})